﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PalcoCentral
{
    public partial class pcPessoaVerDisc : Form
    {
        Database.person p;
        public pcPessoaVerDisc(Database.person p)
        {
            InitializeComponent();
            this.p = p;
            loadData();
        }

        private void loadData()
        {
            Library.OperationStatus os = Database.CourseDBAdapter.getInstance().showPersonCourses(p);
            if (os.sucess)
            {
                List<Database.course> res = (List<Database.course>)os.res;

                listaDisc.DataSource = res;
                listaDisc.Update();
                if (res != null && res.Count > 0)
                {
                    //personDB.DataSource = res;

                }
                else
                {
                    //Library.Utils.atentionFields("Esta disciplina ainda não tem pessoas associadas.", "Atenção");
                }

                if (listaDisc.SelectedIndex > -1)
                {
                    apagarDisc.Enabled = true;
                    imprimirHorario.Enabled = true;
                    delDisc.Enabled = true;
                }
                else
                {
                    apagarDisc.Enabled = false;
                    imprimirHorario.Enabled = false;
                    delDisc.Enabled = false;
                }
            }
        }

        private void pcSelectPessoa_Load(object sender, EventArgs e)
        {

        }

        private void searchSelectPessoasET_KeyDown(object sender, KeyEventArgs e)
        {

        }

        private void searchSelectPessoa_Click(object sender, EventArgs e)
        {
            //Library.OperationStatus os = Database.PersonDBAdapter.getInstance().searchPersons(searchSelectPessoaET.Text);
            //if (os.sucess)
            //{
            //    List<Database.person> res = (List<Database.person>)os.res;
            //    listaDisc.DataSource = res;
            //    listaDisc.Update();
            //    if (res != null && res.Count > 0)
            //    {
            //        //personDB.DataSource = res;

            //    }
            //    else
            //    {
            //        MessageBox.Show("Não forma encontrados resultados.");
            //        //No Results Found
            //    }

            //}
        }

        private void refreshListing(object sender, EventArgs e)
        {
            loadData();
        }

        private void printButton_Click(object sender, EventArgs e)
        {
            if (this.p.personRole.CompareTo("Student") == 0)
            {
                List<Library.CalendarioInput> lci = new List<Library.CalendarioInput>();
                Dictionary<String, ICollection<Database.person>> listaDisciplinasPessoas = new Dictionary<string, ICollection<Database.person>>();
                foreach (var course in (List<Database.course>)Database.CourseDBAdapter.getInstance().showPersonCourses(p).res)
                {

                    foreach (var item in (List<Database.@class>)Database.CourseDBAdapter.getInstance().showClasses(course.courseId).res)
                    {
                        Library.CalendarioInput ci = new Library.CalendarioInput();
                        ci.DayOfWeek = item.dayOfWeek;
                        ci.Start = item.startTime;
                        ci.End = item.startTime + item.duration;

                        if (string.IsNullOrEmpty(course.instrument))
                        {
                            ci.Nome = /*Library.CourseType.getValue(course.courseType).ToString() + ":" + */course.courseName;
                        }
                        else
                        {
                            ci.Nome = /*course.instrument + ":" +*/ course.courseName;
                        }

                        lci.Add(ci);
                    }
                    if (string.IsNullOrEmpty(course.instrument))
                    {

                        listaDisciplinasPessoas.Add(/*Library.CourseType.getValue(course.courseType).ToString() + ":" +*/ course.courseName, course.person);
                    }
                    else
                    {

                        listaDisciplinasPessoas.Add(/*course.instrument + ": " +*/ course.courseName, course.person);
                    }
                }
                pcVerCalend pvc = new pcVerCalend("Horário de " + p.personName, "Horário de " + p.personName + " " + p.personRole + " " + p.personId, lci);
                pvc.listaProfs = listaDisciplinasPessoas;
                pvc.populateCalend();
                pvc.Show();
            }
            else
            {
                List<Library.CalendarioInput> lci = new List<Library.CalendarioInput>();
                Dictionary<String, ICollection<Database.student>> listaDisciplinasPessoas = new Dictionary<string, ICollection<Database.student>>();
                foreach (var course in (List<Database.course>)Database.CourseDBAdapter.getInstance().showPersonCourses(p).res)
                {

                    foreach (var item in (List<Database.@class>)Database.CourseDBAdapter.getInstance().showClasses(course.courseId).res)
                    {
                        Library.CalendarioInput ci = new Library.CalendarioInput();
                        ci.DayOfWeek = item.dayOfWeek;
                        ci.Start = item.startTime;
                        ci.End = item.startTime + item.duration;
                        if (string.IsNullOrEmpty(course.instrument))
                        {
                            ci.Nome = /*Library.CourseType.getValue(course.courseType).ToString() + ":" +*/ course.courseName;
                        }
                        else
                        {
                            ci.Nome = /*course.instrument + ":" +*/ course.courseName;
                        }
                        lci.Add(ci);
                    }

                    if (string.IsNullOrEmpty(course.instrument))
                    {


                        listaDisciplinasPessoas.Add(/*Library.CourseType.getValue(course.courseType).ToString() + ":" +*/ course.courseName, course.student);
                    }
                    else
                    {

                        listaDisciplinasPessoas.Add(/*course.instrument + ":" +*/ course.courseName, course.student);
                    }
                }
                pcVerCalend pvc = new pcVerCalend("Horário de " + p.personName, "Horário de " + p.personName + " " + p.personRole + " " + p.personId, lci);
                pvc.listaStudents = listaDisciplinasPessoas;
                pvc.populateCalend();
                pvc.Show();
            }

        }

        private void verDiscButton_Click(object sender, EventArgs e)
        {
            if (listaDisc.SelectedIndex > -1)
            {
                pcVerDisc vd = new pcVerDisc((Database.course)listaDisc.SelectedItem);
                vd.FormClosed += new FormClosedEventHandler(refreshListing);
                vd.ShowDialog();
            }
        }

        public Database.person selectedPerson // retrieving a value from
        {
            get;
            set;
        }

        private void delDisc_Click(object sender, EventArgs e)
        {
            if (listaDisc.SelectedIndex > -1)
            {
                if (MessageBox.Show("Deseja mesmo desassociar esta disciplina desta pessoa?\n", "Desassociar disciplina de pessoa", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    Library.OperationStatus os = Database.CourseDBAdapter.getInstance().removePersonFromCourse(p, ((Database.course)listaDisc.SelectedItem), Library.PersonRole.Admin);
                    if (os.sucess)
                    {
                        loadData();
                        Library.Utils.sucessFields("Disciplina desassociada com sucesso", "Gerir disciplinas de pessoa");
                    }
                    else
                    {
                        Library.Utils.errorFields("Erro a desassociar disciplina de pessoa.", "Erro a gerir disciplinas de pessoa");
                    }
                }
            }

        }

        private void addDisc_Click(object sender, EventArgs e)
        {
            pcSelectDisc psc = new pcSelectDisc(this.p);

            psc.FormClosed += new FormClosedEventHandler(cselectDisc_FormClosed);
            psc.ShowDialog();
        }

        void cselectDisc_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.loadData();
        }

        private void listaDisc_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listaDisc.SelectedIndex > -1)
            {
                apagarDisc.Enabled = true;
                imprimirHorario.Enabled = true;
                delDisc.Enabled = true;
            }
            else
            {
                apagarDisc.Enabled = false;
                imprimirHorario.Enabled = false;
                delDisc.Enabled = false;
            }
        }

    }
}
